package edu.xpu.project.vehicle.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.xpu.project.log.repository.SchoolBusLogRepository;
import edu.xpu.project.vehicle.entity.SchoolBus;
import edu.xpu.project.vehicle.repository.SchoolBusRepository;

/**
 * @describe SchoolBusService 的业务实现类
 * @author liukang
 * @date 2017年5月9日
 */
@Service
@Transactional(readOnly=true)
public class SchoolBusServiceImpl implements SchoolBusService {
	
	@Autowired
	private SchoolBusRepository schoolBusRepository;
	@Autowired
	private SchoolBusLogRepository busLogRepository;

	@Override
	public SchoolBus get(Long id) {
		return schoolBusRepository.findOne(id);
	}

	@Transactional(readOnly=false)
	@Override
	public SchoolBus saveOrUpdate(SchoolBus t) {
		return schoolBusRepository.saveAndFlush(t);
	}

	@Transactional(readOnly=false)
	@Override
	public void delete(Long id) {
		busLogRepository.deleteByfk(id);
		schoolBusRepository.deleteById(id);
	}

	@Override
	public Page<SchoolBus> getPageByCriterias(String pageNumberStr,
			String pageSizeStr, final SchoolBus schoolBus) {
		Integer pageNumber = 1;
		Integer pageSize = 10;
		try {
			pageNumber = Integer.parseInt(pageNumberStr);
		} catch (Exception e) {}
		try {
			pageSize = Integer.parseInt(pageSizeStr);
		} catch (Exception e) {}
		Pageable pageable = new PageRequest(pageNumber - 1, pageSize, new Sort(Direction.DESC, "createDate"));
		return schoolBusRepository.findAll(new Specification<SchoolBus>() {
			
			@Override
			public Predicate toPredicate(Root<SchoolBus> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				if (schoolBus == null) {
					return null;
				}
				List<Predicate> predicates = new ArrayList<Predicate>(3);
				if (StringUtils.isNotBlank(schoolBus.getName())) {
					predicates.add(cb.like(root.get("name").as(String.class), "%"+schoolBus.getName()+"%"));
				}
				if(schoolBus.getStatus() != null && schoolBus.getStatus() != -1) {
					predicates.add(cb.equal(root.get("status").as(Integer.class), schoolBus.getStatus()));
				}
				if (schoolBus.getSeatTotal() != null) {
					predicates.add(cb.equal(root.get("seatTotal").as(Integer.class), schoolBus.getSeatTotal()));
				}
				if (predicates.size() == 0) {
					return null;
				}
				Predicate[] predArr= new Predicate[predicates.size()];
				return cb.and(predicates.toArray(predArr));
			}
		}, pageable);
	}

	@Override
	public List<SchoolBus> getSchoolBusByStatus(Integer status) {
		return schoolBusRepository.findByStatus(status);
	}

	@Override
	public List<SchoolBus> getSchoolBusByStatusAndCurrentPlace(Integer status, Integer currentPlace) {
		return schoolBusRepository.findByStatusAndLocalStatus(status, currentPlace);
	}

	@Transactional(readOnly=false)
	@Override
	public void delete(SchoolBus schoolBus) {
		schoolBusRepository.delete(schoolBus);
	}

}
